package ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {

    int[][] data = new int[4][4];
    int[][] win = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    int step;
    JMenu functionJMenu = new JMenu("功能");
    JMenu aboutJMenu = new JMenu("关于我们");

    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem exitItem = new JMenuItem("关闭游戏");

    JMenuItem accountItem = new JMenuItem("公众号");

    public GameJFrame() {
        // 初始化界面
        initJFrame();
        // 初始化菜单
        initJMenuBar();

        // 初始化数据
        initData();


        //初始化图片
        initImage();

        this.setVisible(true);
    }

    // 记录空白方块在二维数组中的位置
    int x;
    int y;
    String path = "image\\animal\\animal3\\";
    private void initData() {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random rand = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int index = rand.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }

        for (int i = 0; i < tempArr.length; i++) {
            if(tempArr[i] == 0){
                x=i/4;
                y=i%4;
            }
            data[i / 4][i % 4] = tempArr[i];
        }

        //
    }

    private void initImage() {

        // 清空已经出现的所有图片
        this.getContentPane().removeAll();
        if(victory()){
            JLabel winLabel = new JLabel(new ImageIcon("image\\win.png"));
            winLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winLabel);
        }
        JLabel stepLabel = new JLabel(step + "步数");
        stepLabel.setBounds(50, 30, 100, 20);
        this.getContentPane().add(stepLabel);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                // 创建一个ImageIcon的对象
                //创建一个管理容器JLabel
                JLabel jLable = new JLabel(new ImageIcon( path + num + ".jpg"));
                // 指定图片位置
                jLable.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                jLable.setBorder(new BevelBorder(1));
                // 把管理容器添加到隐藏容器中
                this.getContentPane().add(jLable);
            }
        }

        JLabel jLabel = new JLabel(new ImageIcon("image\\background.png"));
        jLabel.setBounds(40,40,508,560);
        this.getContentPane().add(jLabel);

        // 刷新界面
        this.getContentPane().repaint();
    }

    private void initJMenuBar() {
        JMenuBar jMenuBar = new JMenuBar();

        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(exitItem);

        aboutJMenu.add(accountItem);

        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        this.setJMenuBar(jMenuBar);

        // 绑定事件
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        exitItem.addActionListener(this);
        accountItem.addActionListener(this);
    }

    private void initJFrame() {
        this.setSize(603, 680);
        // 设置标题
        this.setTitle("拼图");
        // 界面置顶
        this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        // 设置关闭模式
        this.setDefaultCloseOperation(3);
        // 取消默认居中放置
        this.setLayout(null);

        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(victory()){
            return;
        }
        int key = e.getKeyCode();
        if(key == 65){
            this.getContentPane().removeAll();
            JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            JLabel background = new JLabel(new ImageIcon("image\\background.png"));
            background.setBounds(40,40,508,560);
            this.getContentPane().add(background);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
    // 对上下左右判断
       int code = e.getKeyCode();
       if(code == 37){
           if(y==3){
               return;
           }
           System.out.println("向左移动");
           data[x][y] = data[x][y+1];
           data[x][y+1] = 0;
           y++;
           step++;
           initImage();
       } else if (code==38) {
           if(x==3){
               return;
           }
           System.out.println("向上移动");
           data[x][y] = data[x+1][y];
           data[x+1][y] = 0;
           x++;
           step++;
           initImage();
       }else if(code==39){
           if(y==0){
               return;
           }
           System.out.println("向右移动");
           data[x][y] = data[x][y-1];
           data[x][y-1] = 0;
           y--;
           step++;
           initImage();
       }else if(code==40){
           if(x==0){
               return;
           }
           System.out.println("向下移动");
           data[x][y] = data[x-1][y];
           data[x-1][y] = 0;
           x--;
           step++;
           initImage();
       } else if (code==65) {
           initImage();
       }else if(code == 87){
           data = new int[][]{
                   {1,2,3,4},
                   {5,6,7,8},
                   {9,10,11,12},
                   {13,14,15,0}
           };
           initImage();
           x = 3;
           y = 3;
       }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == replayItem){
            System.out.println("重新游戏");
            step = 0;
            initData();
            initImage();

        }else if(obj == reLoginItem){
            System.out.println("重新登录");
            this.setVisible(false);
            new LoginJFrame();
        }else if(obj == exitItem){
            System.out.println("关闭游戏");
            System.exit(0);
        }else if(obj == accountItem){
            System.out.println("公众号");
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("image\\about.png"));
            jLabel.setBounds(0,0,258,258);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(344,344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }
    }

    public boolean victory(){
        for(int i = 0; i < data.length; i++){
            for(int j = 0; j < data[i].length; j++){
                if(data[i][j] != win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

}
